﻿using System;
using System.Collections.Generic;
using System.Data.Objects.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums;
using ZBJF.Warranty.Common.Enums.InsuranceOrder;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Order;
using ZBJF.Warranty.Model.ViewModel.Product;

namespace ZBJF.Warranty.BLL
{
    public partial class CustomerServiceAndInsuranceOrderMappingService
    {
        /// <summary>
        /// 获取任务数量最少的客服信息
        /// </summary>
        /// <param name="customerServiceId">客服ID</param>
        /// <returns></returns>
        public string GetLessTaskCustomerServicePhone(ref int customerServiceId)
        {
            var temp = (from a in ServiceFactory.SystemUserServiceInstance.LoadEntities(t => t.IsAvailable == true && t.BelongApplicationType == (byte)ApplicationType.CustomerServiceSite && t.UserPhone != null && t.IsCheckOnline == 1)
                        join b in LoadEntities(t => t.CheckResult == (byte)CheckResultEnum.NoCheck) on a.ID equals b.CustomerServiceID into temp1
                        from c in temp1.DefaultIfEmpty()
                        group c by new { a.ID, a.UserPhone } into temp2
                        orderby temp2.Count(m => temp2.Key.ID > 0)
                        select new { CustomerServiceID = temp2.Key.ID, TaskCount = temp2.Count(m => temp2.Key.ID > 0), Phone = temp2.Key.UserPhone }).FirstOrDefault();

            if (temp == null)
                return null;
            customerServiceId =Convert.ToInt32(temp.CustomerServiceID);
            return temp.Phone;

        }
    }
}
